<?php

require_once(PATH_CLASS.'widget.class.php');
require_once(PATH_CLASS.'commun.class.php');

class pageSwitcher extends widget{
  
  const CLASS_NAME  = 'pageSwitcher';
  static $switchers = array();
  
  public static function addSwitcherURL(&$params,$url=''){
    if(!empty($url)){
      $params['url']=$url;
    }    
  }
  public static function addSwitcherParam(&$params,$lib,$value,$url='',$default=false){
    if(!is_array($params)){
      $params = array();
    }
    
    $def = array(
        'checked'=>''
        ,'datas'=>array()
        ,'switcher'=>'type'
        ,'url'=>''
        ,'value'=>&$_GET
    );
    
    $params=  array_merge($def,$params);
            
    $params['datas'][] = array('lib'=>$lib,'value'=>$value,'default'=>$default);
    
    if($default){
      $params['checked']=$value;
    }
    
    self::addSwitcherURL($params,$url);
    
  }

  public static function addSwitcher($name,$params){
    self::$switchers[$name] = $params;
  }

  public static function getSwitcherParams($name){
    $ret = array();
    if(isset(self::$switchers[$name])){
      $ret = self::$switchers[$name];
    }
    return $ret;
  }
  
  public function getDefaults() {
    $ret = parent::getDefaults();
    $ret = array_merge(array(
      'class' => ''
    ),$ret);
    
    return $ret;
  }
  
  public function autoRenderJs() {
    return true;
  }
  
  public function autoRenderCss() {
    return true;
  }

  public function __construct($params) {
    parent::__construct($params);
  }
  
  protected function _get_called_class() { return self::CLASS_NAME;  }

  public function _render() {
    $params = $this->params;
    $layout = '';
    
    if(isset($params['datas'])){
      $_GET['prout']='prout';
      $params['switcherParams'] = array2Obj(self::getSwitcherParams($params['datas']));
      $layout = $this->renderTplFromFile(__FILE__, $params);
    }
    
    
    return $layout;
  }
}

